package com.xhwl.centiotdata.suppliersystem.lifang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.DaHuaConstant;
import com.xhwl.centiotdata.pojo.constant.LiFangConstant;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.centiotdata.pojo.dto.haikang.DoorHaiKangDTO;
import com.xhwl.centiotdata.pojo.dto.lifang.DeviceLiFangDTO;
import com.xhwl.centiotdata.pojo.dto.lifang.DoorLiFangDTO;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 16:26 2022/7/11
 */
@Slf4j
@Service
public class DeviceLiFangHandler {

    @Autowired
    private GeneralLiFangHandler generalLiFangHandler;


    public List<DeviceLiFangDTO> pullDevice(DeviceIntegrationConfigDTO configDTO) {
        Map<String, Object> param = new HashMap<>();
        JSONObject resultObject = generalLiFangHandler.post(LiFangConstant.QUERY_DEVICE, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        if (resultObject == null) {
            return null;
        }
        Integer result = resultObject.getInteger("result");
        if(result != 200){
            log.error("post path:{};errMsg:{}", LiFangConstant.QUERY_DEVICE, resultObject.getString("resultInfo"));
            return null;
        }
        String obj = resultObject.getString("obj");
        if(StringUtils.isEmpty(obj)){
            log.error("post path:{};data is null!", LiFangConstant.QUERY_DEVICE);
            return null;
        }
        List<DeviceLiFangDTO> deviceLiFangDTOList = JSONObject.parseArray(obj, DeviceLiFangDTO.class);
        if (CollectionUtils.isEmpty(deviceLiFangDTOList)) {
            return null;
        }
        return deviceLiFangDTOList;
    }

    public List<DoorLiFangDTO> pullDoor(DeviceIntegrationConfigDTO configDTO) {
        Map<String, Object> param = new HashMap<>();
        param.put("curPage",1);
        param.put("pageSize",1000000);
        JSONObject resultObject = generalLiFangHandler.post(LiFangConstant.QUERY_DOOR, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        param.put("devType",1);
        JSONObject resultObject1 = generalLiFangHandler.post(LiFangConstant.QUERY_DOOR, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        if (resultObject == null || null == resultObject1) {
            return null;
        }
        Integer result = resultObject.getInteger("result");
        if(result != 200 || resultObject1.getInteger("result") != 200){
            log.error("post path:{};errMsg:{}", LiFangConstant.QUERY_DOOR, resultObject.getString("resultInfo"));
            return null;
        }
        JSONObject obj = resultObject.getJSONObject("obj");
        if(Objects.isNull(obj) || Objects.isNull(resultObject1.getJSONObject("obj"))){
            log.error("post path:{};data is null!", LiFangConstant.QUERY_DOOR);
            return null;
        }
        String list = obj.getString("list");
        String listFace = resultObject1.getJSONObject("obj").getString("list");
        if(StringUtils.isEmpty(list) && StringUtils.isEmpty(listFace)){
            log.error("post path:{};data is null!", LiFangConstant.QUERY_DOOR);
            return null;
        }
        List<DoorLiFangDTO> doorLiFangDTOList = JSONObject.parseArray(list, DoorLiFangDTO.class);
        List<DoorLiFangDTO> faceDoorLiFangList = JSONObject.parseArray(listFace,DoorLiFangDTO.class);
        doorLiFangDTOList.addAll(faceDoorLiFangList);
        if (CollectionUtils.isEmpty(doorLiFangDTOList)) {
            return null;
        }
        return doorLiFangDTOList;
    }
    public List<DeviceLiFangDTO> pullFaceDevice(DeviceIntegrationConfigDTO configDTO){
        Map<String, Object> param = new HashMap<>();
        JSONObject resultObject = generalLiFangHandler.post(LiFangConstant.QUERY_FACE_DEVICE, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        if (resultObject == null) {
            return null;
        }
        Integer result = resultObject.getInteger("result");
        if(result != 200){
            log.error("post path:{};errMsg:{}", LiFangConstant.QUERY_FACE_DEVICE, resultObject.getString("resultInfo"));
            return null;
        }
        String obj = resultObject.getString("obj");
        if(StringUtils.isEmpty(obj)){
            log.error("post path:{};data is null!", LiFangConstant.QUERY_FACE_DEVICE);
            return null;
        }
        List<DeviceLiFangDTO> deviceLiFangDTOList = JSONObject.parseArray(obj, DeviceLiFangDTO.class);
        if (CollectionUtils.isEmpty(deviceLiFangDTOList)) {
            return null;
        }
        return deviceLiFangDTOList;
    }


    public JSONObject pullDeviceStatus(DeviceIntegrationConfigDTO configDTO) {
        Map<String, Object> param = new HashMap<>();
        JSONObject resultObject = generalLiFangHandler.post(LiFangConstant.QUERY_DOOR_STATUS, param, configDTO.getProjectId(), configDTO.getProductId(), configDTO.getIpAndPort(), configDTO.getLoginName(), configDTO.getPassword());
        if (resultObject == null) {
            return null;
        }
        Integer result = resultObject.getInteger("result");
        if(result != 200){
            log.error("post path:{};errMsg:{}", LiFangConstant.QUERY_DOOR_STATUS, resultObject.getString("resultInfo"));
            return null;
        }
        JSONObject data = resultObject.getJSONObject("resultData");
        if(CollectionUtils.isEmpty(data)){
            log.error("post path:{};data is null!", LiFangConstant.QUERY_DOOR_STATUS);
            return null;
        }

        return data;
    }
}
